package com.shijiu.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.pojo.Sign;
import com.shijiu.pojo.User;
import com.shijiu.service.ShopService;
import com.shijiu.service.SignService;
import com.shijiu.service.UserService;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author xlj
 * @Date Created in  2023/4/20 9:52
 * @DESCRIPTION:
 * @Version V1.0
 */

@RestController
@RequestMapping("sign")
@Transactional(rollbackFor = Exception.class)
public class SignController {
    @Resource
    private SignService signService;

    @Resource
    private UserService userService;

    @Resource
    private ShopService shopService;
    @ApiOperation("定位打卡列表")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("positioning")
    public Result getOrderList(@RequestBody Sign sign){
        //拿到商家id
        String username = sign.getUsername();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        User user = userService.getOne(wrapper);
        Integer userId = user.getId();
        Integer shopId = shopService.getShopId(userId);
        Result result = signService.saveOrUpdate(shopId, sign);
        return result;
    }
    @ApiOperation("打卡详情信息列表")
    @PostMapping("messageByPage")
    public Result messageByPage(@RequestBody Map<String, Object> requestBodyMaps){
        //拿到商家id
        return signService.selectOrderListByPage(requestBodyMaps);
    }
    @ApiOperation("admin 更新商家打卡信息列表")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("updateMessage")
    public Result updateMessage(@RequestBody @Validated Sign sign){
        //拿到商家id
        boolean updateById = signService.updateById(sign);
        if (updateById == false) {
            return Result.fail(Integer.parseInt("500"),"更新失败!");
        }
        return Result.success(updateById);
    }
    @ApiOperation("admin 更新商家打卡信息列表")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("deleteById")
    public Result deleteById(@RequestBody @Validated Sign sign){
        //拿到商家id
        Sign signMessage = new Sign();
        BeanUtil.copyProperties(sign, signMessage,sign.getIsdeleted());
        signMessage.setId(sign.getId());
        signMessage.setIsdeleted("1");
        if (StrUtil.isBlank(sign.getComments())){
            LambdaQueryWrapper<Sign> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Sign::getId,sign.getId());
            Sign serviceOne = signService.getOne(wrapper);
            signMessage.setComments(serviceOne.getComments());
        }
        boolean updateById = signService.updateById(signMessage);
        if (updateById == false) {
            return Result.fail(Integer.parseInt("500"),"删除失败!");
        }
        return Result.success(updateById);
    }
}
